/**
 * Class: DateTime
 *
 * Purpose: Date & Time constants & manipulators
 *
 * @author Ken Scott
 */
package org.openfantasia.util;

import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

public class DateTime {

	public static Date NOW = new Date();

	public static final DateFormat STD_DATE_FORMAT = new SimpleDateFormat("yyyy-MM-dd");

	public static final DateFormat STD_DATE_TIME_FORMAT = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.SSS");

	public static final DateFormat MONTHLY_DATE_FORMAT = new SimpleDateFormat("yyyy-MM");

	public static final DateFormat WEEKLY_DATE_FORMAT = new SimpleDateFormat("yyyy-MM-W");

	public static final DateFormat COMPACT_DATE_TIME_FORMAT = new SimpleDateFormat("yyyyMMdd-HHmmss.SSS");

	public static final DateFormat FULL_DATE_TIME_FORMAT = new SimpleDateFormat("yyyy-MM-dd-HH-mm-ss.SSS");

	public static final long SECOND = 1000L;

	public static final long MINUTE = 60L * SECOND;

	public static final long HOUR = 60L * MINUTE;

	public static final long DAY = 24L * HOUR;

	public static final long WEEK = 7L * DAY;

	public static final long MONTH = 30L * DAY;

	public static final long TWO_MONTHS = 60L * DAY;

	public static final long THREE_MONTHS = 90L * DAY;

	public static final long FOUR_MONTHS = 120L * DAY;

	public static final long SIX_MONTHS = 180L * DAY;

	public static final long NINE_MONTHS = 270L * DAY;

	public static final long YEAR_360 = 360L * DAY;

	public static final long YEAR = 365L * DAY;

	public static final int DATE_UNMODIFIED = 0;

	public static final int DATE_FUTURE = 1;

	public static final int DATE_PAST = 2;

	public static final int DATE_FUTURE_AND_PAST = 3;


	public static void waitNanoseconds(long ns) {
		if (ns <= 0) {
			return;
		}

		try {
			long ms = ns / 1000000L;
			Thread.sleep(ms, (int) (ns % 1000000L));
		}
		catch (InterruptedException e) {
		}
	}


	public static void waitMicroseconds(long us) {
		waitNanoseconds(us * 1000L);
	}


	public static void waitMilliseconds(long ms) {
		if (ms <= 0) {
			return;
		}

		try {
			Thread.sleep(ms);
		}
		catch (InterruptedException e) {
		}
	}


	public static void waitSeconds(long s) {
		waitMilliseconds((long) (s * 1e3));
	}


	public static Date randomizeDate(int type, long range) {
		return randomizeDate(new Date(), type, range);
	}


	public static Date randomizeDate(Date date, int type, long range) {
		if (date == null) {
			date = new Date();
		}
		Calendar cal = Calendar.getInstance();
		long ref = date.getTime();
		double rand = RandomUtils.randomDouble();
		switch (type) {
			case DATE_FUTURE:
				cal.setTimeInMillis(ref + (long) (rand * (double) range));
				break;

			case DATE_PAST:
				cal.setTimeInMillis(ref - (long) (rand * (double) range));
				break;

			case DATE_FUTURE_AND_PAST:
				rand *= 2.0;
				rand -= 1.0;
				cal.setTimeInMillis(ref + (long) (rand * (double) range));
				break;

			case DATE_UNMODIFIED:
			default:
				cal.setTimeInMillis(date.getTime());
				break;
		}
		return cal.getTime();
	}
}
